//
//  AppGuideViewController.m
//  WeMeet
//
//  Created by wkx on 13-9-2.
//  Copyright (c) 2013年 wkx. All rights reserved.
//

#import "AppGuideViewController.h"
#import "AppDelegate.h"
@interface AppGuideViewController ()
{
    UIScrollView *_scrollView;
}
@end

@implementation AppGuideViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    

}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)startGuide
{
    /*
    NSNumber* object = [[NSUserDefaults standardUserDefaults] objectForKey:K_UserDefault_IsShowAppGuideImage];
    if (object == nil || [object boolValue] == YES) {
        [self initResource];
    }
     */
    [self initResource];
}
#pragma mark-- UI
- (void)initResource
{
    
    //ScrollView
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (IOS7) {
        [_scrollView setFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT)];
    }
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
	_scrollView.showsVerticalScrollIndicator = NO;
	_scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.delegate = self;
    NSMutableArray *arrayGuideImageName = [[NSMutableArray alloc] init];
    if (iPhone5) {
        [arrayGuideImageName addObject:@"guide1-1136"];
        [arrayGuideImageName addObject:@"guide2-1136"];
        [arrayGuideImageName addObject:@"guide3-1136"];
    }
    else
    {
        [arrayGuideImageName addObject:@"guide1-960"];
        [arrayGuideImageName addObject:@"guide2-960"];
        [arrayGuideImageName addObject:@"guide3-960"];
        
    }
    for (int i = 0; i < [arrayGuideImageName count]; i++) {
		UIView *pageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
		UIImageView *pageContentView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [pageView addSubview:pageContentView];
		
        if (i == arrayGuideImageName.count-1) {
            //button
            UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            //[startBtn setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT+20)];
            [startBtn setFrame:CGRectMake((SCREEN_WIDTH-261)/2.0f, SCREEN_HEIGHT-80, 261, 41)];
            [startBtn setBackgroundColor:[UIColor clearColor]];
            [startBtn setBackgroundImage:[UIImage imageNamed:@"guide_btn_start_icon"] forState:UIControlStateNormal];
            [startBtn setBackgroundImage:[UIImage imageNamed:@"guide_btn_start_clicked_icon"] forState:UIControlStateHighlighted];
            [startBtn addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
            [pageView addSubview:startBtn];
        }
        
		// pageview 的位置
		CGRect rectPageView = pageView.frame;
		rectPageView.origin.x = i * pageView.frame.size.width;
		pageView.frame = rectPageView;
		[_scrollView addSubview:pageView];
		
		// page view 内容
		NSString *strImageName = [arrayGuideImageName objectAtIndex:i];
		UIImage *image = [UIImage imageNamed:strImageName];
		pageContentView.image = image;
	}
	_scrollView.contentSize = CGSizeMake(SCREEN_WIDTH* [arrayGuideImageName count], SCREEN_HEIGHT );
	[self.view addSubview:_scrollView];

    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
    [appDelegate.window addSubview:self.view];
}

#pragma mark-- Action
- (void)clickButton
{
    [UIView animateWithDuration:1.0f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseIn animations:^{
                            self.view.alpha = 0.0f;
                        } completion:^(BOOL finished) {
                            [self.view removeFromSuperview];
                            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:NO] forKey:K_UserDefault_IsShowAppGuideImage];
                            [[NSUserDefaults standardUserDefaults] synchronize];
                        }];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //_pf(scrollView.contentOffset.x);
    if (scrollView.contentOffset.x/SCREEN_WIDTH == 1) {
        [self clickButton];
    }
}
@end
